package com.richard.base.view

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.richard.base.R
import me.yokeyword.fragmentation.SupportFragment

/**
 ***************************************
 * 项目名称:Giggle
 * @Author wuzhiguo
 * 邮箱：wuzhiguo@ksjgs.com
 * 创建时间: 2020/7/29     10:21 AM
 * 用途:
 ***************************************
 */

class GToolBar(
    context: Context,
    attributeSet: AttributeSet?
) :
    FrameLayout(context, attributeSet) {
    private lateinit var backLayout: FrameLayout
    private lateinit var rightLayout: FrameLayout
    private lateinit var fl_root: FrameLayout
    private lateinit var backImageView: ImageView
    private lateinit var rightImageView: ImageView
    private lateinit var titleTextView: TextView
    private lateinit var rightTextView: TextView
    private lateinit var viewStub: RelativeLayout
    private val backIconRes: Int
    private var rightIconRes: Int
    private val titleColorRes: Int
    private var title: String?
    private var rightTxt: String?
    private var rightView: Int
    private val backgroundRes: Int
    private val isViewStubInflated = false
    private fun initViews() {
        LayoutInflater.from(context).inflate(R.layout.base_widget_new_tool_bar, this)
        backLayout = findViewById(R.id.new_toolbar_back)
        rightLayout = findViewById(R.id.new_toolbar_right)
        backImageView = findViewById(R.id.new_toolbar_back_img)
        titleTextView = findViewById(R.id.new_toolbar_title)
        viewStub = findViewById(R.id.new_toolbar_stub_view)
        fl_root = findViewById(R.id.fl_root)
        if (!TextUtils.isEmpty(title)) {
            titleTextView.text = title
        }
        titleTextView.setTextColor(context.resources.getColor(titleColorRes))
        fl_root.setBackgroundResource(backgroundRes)
        if (rightView != 0) {
            createViewStub()
        }
        backImageView.setImageResource(backIconRes)
//        backLayout.setOnClickListener {
//            var fragment = context as SupportFragment
//            fragment.pop()
//        }
    }

    fun setRightOnClickListener(onClickListener: OnClickListener?) {
        rightLayout.setOnClickListener(onClickListener)
    }

    fun setTitle(title: String?) {
        this.title = title
        titleTextView.text = title
    }

    fun setRightTxt(text: String?) {
        rightTxt = text
        rightView = RIGHT_VIEW_TEXT
        createViewStub()
    }

    fun setRightIcon(resId: Int) {
        rightIconRes = resId
        rightView = RIGHT_VIEW_IMAGE
        createViewStub()
    }

    fun setBackIcon(resId: Int) {
        backImageView.setImageResource(resId)
    }

    fun setBackOnClickListener(onClickListener: OnClickListener?) {
        backLayout.setOnClickListener(onClickListener)
    }

    private fun createViewStub() {
        if (rightView == RIGHT_VIEW_TEXT) {
            val toolbarTextView: View =
                LayoutInflater.from(context).inflate(R.layout.base_g_tool_bar_textview, null)
            viewStub.removeAllViews()
            viewStub.addView(toolbarTextView)
            if (rightTxt != null) {
                rightTextView = toolbarTextView.findViewById(R.id.new_toolbar_right_txt)
                rightTextView.setText(rightTxt)
                rightTextView.setTextColor(context.resources.getColor(titleColorRes))
            }
        } else if (rightView == RIGHT_VIEW_IMAGE) {
            val toolbarImageView: View =
                LayoutInflater.from(context).inflate(R.layout.base_g_tool_bar_imageview, null)
            viewStub.removeAllViews()
            viewStub.addView(toolbarImageView)
            if (rightIconRes != 0) {
                rightImageView =
                    toolbarImageView.findViewById(R.id.new_toolbar_right_img)
                rightImageView.setImageResource(rightIconRes)
            }
        }
    }

    companion object {
        const val RIGHT_VIEW_TEXT = 2
        const val RIGHT_VIEW_IMAGE = 1
    }

    init {
        val a = context.obtainStyledAttributes(attributeSet, R.styleable.GToolBar)
        backIconRes = a.getResourceId(R.styleable.GToolBar_bar_back_icon, R.mipmap.icon_close_carton)
        rightIconRes = a.getResourceId(R.styleable.GToolBar_bar_right_icon, 0)
        titleColorRes = a.getResourceId(R.styleable.GToolBar_bar_title_color, R.color.base_color_666666)
        title = a.getString(R.styleable.GToolBar_bar_title)
        rightTxt = a.getString(R.styleable.GToolBar_bar_right_text)
        rightView = a.getInt(R.styleable.GToolBar_bar_right_view, 0)
        backgroundRes = a.getResourceId(R.styleable.GToolBar_bar_background_color, R.color.base_color_ffffff)
        a.recycle()
        initViews()
    }
}
